package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DatabaseMng;
import com.ruoyi.system.service.IDatabaseMngService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据源管理Controller
 *
 * @author weicc
 * @date 2023-09-23
 */
@RestController
@RequestMapping("/system/database")
public class DatabaseMngController extends BaseController {
    @Autowired
    private IDatabaseMngService databaseMngService;

    /**
     * 查询数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:database:list')")
    @GetMapping("/list")
    public TableDataInfo list(DatabaseMng databaseMng) {
        startPage();
        List<DatabaseMng> list = databaseMngService.selectDatabaseMngList(databaseMng);
        return getDataTable(list);
    }

    /**
     * 导出数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:database:export')")
    @Log(title = "数据源管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DatabaseMng databaseMng) {
        List<DatabaseMng> list = databaseMngService.selectDatabaseMngList(databaseMng);
        ExcelUtil<DatabaseMng> util = new ExcelUtil<DatabaseMng>(DatabaseMng.class);
        util.exportExcel(response, list, "数据源管理数据");
    }

    /**
     * 获取数据源管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:database:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(databaseMngService.selectDatabaseMngById(id));
    }

    /**
     * 新增数据源管理
     */
    @PreAuthorize("@ss.hasPermi('system:database:add')")
    @Log(title = "数据源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DatabaseMng databaseMng) {
        return toAjax(databaseMngService.insertDatabaseMng(databaseMng));
    }

    /**
     * 修改数据源管理
     */
    @PreAuthorize("@ss.hasPermi('system:database:edit')")
    @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatabaseMng databaseMng) {
        return toAjax(databaseMngService.updateDatabaseMng(databaseMng));
    }

    /**
     * 删除数据源管理
     */
    @PreAuthorize("@ss.hasPermi('system:database:remove')")
    @Log(title = "数据源管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(databaseMngService.deleteDatabaseMngByIds(ids));
    }

    /**
     * 测试数据源连接
     *
     * @param databaseMng
     * @return
     */
    @PostMapping("verify")
    public AjaxResult verifyDataSource(@RequestBody DatabaseMng databaseMng) {
        Boolean b = databaseMngService.verifyDataSource(databaseMng);
        if (b) {
            return AjaxResult.success("connected successfully");
        }
        return AjaxResult.error("connected failed");
    }

    /**
     * 同步数据源
     * @param dbId
     * @return
     */
    @GetMapping("/synchronize")
    public AjaxResult synchronizeDataSource(@RequestParam String dbId) {
        databaseMngService.synchronizeDataSource(dbId);
        return AjaxResult.success();
    }
}
